---
title: "Class Experiences"
subtitle: "Code to create figures in manuscript and supporting information"
output: html_document
editor_options: 
  chunk_output_type: console
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(
	echo = FALSE,
	message = FALSE,
	warning = FALSE
)

knitr::opts_knit$set(
	root.dir= "C:/" 
)

setwd("C:/")

library(tidyverse)
library(stringr)
library(ggplot2)
library(cowplot)
library(sjPlot)
library(hrbrthemes)
hrbrthemes::import_roboto_condensed()
library(haven)

# Theme setup
theme_set(theme_ipsum_rc())
theme_update(plot.margin= unit(c(0.1,0.1,0.1,0.1), "cm"))

save_plot = sjPlot::save_plot
```

## Figure 1

### EGP class, main model specification

```{r}

fig1= read_dta(paste0("figs/fig1_z_egp_main_75_all.dta"))

fig1 = fig1 %>% mutate(class=factor(class,levels=c(
                                                1,
                                                2,
                                                3,
                                                4,
                                                5),
                                    labels=c("Worker",
                                             "Routine non-manual (vs Worker)",
                                             "Lower Service (vs Worker)",
                                             "Higher service (vs Worker)",
                                             "Small business owners and farmers (vs Worker)")),
                       model=fct_rev(factor(plotS,
                                            levels=c("Father's class",
                                                     "Current class",
                                                     "Stable class during BHPS",
                                                     "Stable class over lifetime"),
                                      labels=c("Class as ...Father's class",
                                               "...Current class",
                                               "...Stable class during BHPS",
                                               "...Stable class over lifetime"))))

fig1 %>%  filter(class!="Worker") %>% 
  ggplot(aes(x=model, y=b,ymin=ll1,ymax=ul1,color=model,fill=model)) +
  coord_flip()+
  facet_wrap(~class,ncol=1)+
  geom_hline(aes(yintercept = 0), color="gray20")+
  geom_point()+
  geom_errorbar(size=1,alpha=0.7, width=0,position=position_dodge(width = 0.5)) +
  scale_y_continuous(name="Economic values (right to left)",breaks=seq(-1,0,by=0.2),limits=c(-1,0)) +
  scale_x_discrete(name="") +
  scale_color_brewer(palette="Set1") +
  theme(legend.position="none",
        panel.spacing = unit(1, "lines"),
        panel.grid.major.x = element_line(colour = "grey90"),
        panel.grid.major.y = element_blank(),
        panel.grid.minor = element_blank())

save_plot("figs/fig1.png",width=12,height=12, dpi=350)
```

### EGP class, 90% threshold for class membership (Appendix B3)

```{r}

fig1= read_dta(paste0("figs/fig1_z_egp_main_90_all.dta"))

fig1 = fig1 %>% mutate(class=factor(class,levels=c(
                                                1,
                                                2,
                                                3,
                                                4,
                                                5),
                                    labels=c("Worker",
                                             "Routine non-manual (vs Worker)",
                                             "Lower Service (vs Worker)",
                                             "Higher service (vs Worker)",
                                             "Small business owners and farmers (vs Worker)")),
                       model=fct_rev(factor(plotS,
                                            levels=c("Father's class",
                                                     "Current class",
                                                     "Stable class during BHPS",
                                                     "Stable class over lifetime"),
                                      labels=c("Class as ...Father's class",
                                               "...Current class",
                                               "...Stable class during BHPS",
                                               "...Stable class over lifetime"))))

fig1 %>%  filter(class!="Worker") %>% 
  ggplot(aes(x=model, y=b,ymin=ll1,ymax=ul1,color=model,fill=model)) +
  coord_flip()+
  facet_wrap(~class,ncol=1)+
  geom_hline(aes(yintercept = 0), color="gray20")+
  geom_point()+
  geom_errorbar(size=1,alpha=0.7, width=0,position=position_dodge(width = 0.5)) +
  scale_y_continuous(name="Economic values (right to left)",breaks=seq(-2,0,by=0.2),limits=c(-1.1,0)) +
  scale_x_discrete(name="") +
  scale_color_brewer(palette="Set1") +
  theme(legend.position="none",
        panel.spacing = unit(1, "lines"),
        panel.grid.major.x = element_line(colour = "grey90"),
        panel.grid.major.y = element_blank(),
        panel.grid.minor = element_blank())

save_plot("figs/appendix_fig1_B3.png",width=12,height=12, dpi=350)
```


### EGP class, only long-term panel members (Appendix B4) 

```{r}
fig1= read_dta(paste0("figs/fig1_z_egp_main_75_fullpan.dta"))

fig1 = fig1 %>% mutate(class=factor(class,levels=c(
                                                1,
                                                2,
                                                3,
                                                4,
                                                5),
                                    labels=c("Worker",
                                             "Routine non-manual (vs Worker)",
                                             "Lower Service (vs Worker)",
                                             "Higher service (vs Worker)",
                                             "Small business owners and farmers (vs Worker)")),
                       model=fct_rev(factor(plotS,
                                            levels=c("Father's class",
                                                     "Current class",
                                                     "Stable class during BHPS",
                                                     "Stable class over lifetime"),
                                      labels=c("Class as ...Father's class",
                                               "...Current class",
                                               "...Stable class during BHPS",
                                               "...Stable class over lifetime"))))

fig1 %>%  filter(class!="Worker") %>% 
  ggplot(aes(x=model, y=b,ymin=ll1,ymax=ul1,color=model,fill=model)) +
  coord_flip()+
  facet_wrap(~class,ncol=1)+
  geom_hline(aes(yintercept = 0), color="gray20")+
  geom_point()+
  geom_errorbar(size=1,alpha=0.7, width=0,position=position_dodge(width = 0.5)) +
  scale_y_continuous(name="Economic values (right to left)",breaks=seq(-1,0,by=0.2),limits=c(-1,0)) +
  scale_x_discrete(name="") +
  scale_color_brewer(palette="Set1") +
  theme(legend.position="none",
        panel.spacing = unit(1, "lines"),
        panel.grid.major.x = element_line(colour = "grey90"),
        panel.grid.major.y = element_blank(),
        panel.grid.minor = element_blank())

save_plot("figs/appendix_fig1_B4.png",width=12,height=12, dpi=350)
```

### EGP class, restrict sample to after 8 waves in employment (Appendix B5)
```{r}
fig1= read_dta(paste0("figs/fig1_z_egp_main_75res_all.dta"))
fig1 = fig1 %>% mutate(class=factor(class,levels=c(
                                                1,
                                                2,
                                                3,
                                                4,
                                                5),
                                    labels=c("Worker",
                                             "Routine non-manual (vs Worker)",
                                             "Lower Service (vs Worker)",
                                             "Higher service (vs Worker)",
                                             "Small business owners and farmers (vs Worker)")),
                       model=fct_rev(factor(plotS,
                                            levels=c("Father's class",
                                                     "Current class",
                                                     "Stable class during BHPS",
                                                     "Stable class over lifetime"),
                                      labels=c("Class as ...Father's class",
                                               "...Current class",
                                               "...Stable class during BHPS",
                                               "...Stable class over lifetime"))))
fig1 %>%  filter(class!="Worker") %>%
  ggplot(aes(x=model, y=b,ymin=ll1,ymax=ul1,color=model,fill=model)) +
  coord_flip()+
  facet_wrap(~class,ncol=1)+
  geom_hline(aes(yintercept = 0), color="gray20")+
  geom_point()+
  geom_errorbar(size=1,alpha=0.7, width=0,position=position_dodge(width = 0.5)) +
  scale_y_continuous(name="Economic values (right to left)",breaks=seq(-1,0,by=0.2),limits=c(-1.05,0)) +
  scale_x_discrete(name="") +
  scale_color_brewer(palette="Set1") +
  theme(legend.position="none",
        panel.spacing = unit(1, "lines"),
        panel.grid.major.x = element_line(colour = "grey90"),
        panel.grid.major.y = element_blank(),
        panel.grid.minor = element_blank())
save_plot("figs/appendix_fig1_B5.png",width=12,height=12, dpi=350)
```



### EGP class, main model specification (Appendix B6)

```{r}

fig1= read_dta(paste0("figs/fig1_z_egp_main_75_all_B6.dta"))

fig1 = fig1 %>% mutate(class=factor(class,levels=c(
                                                1,
                                                2,
                                                3,
                                                4,
                                                5),
                                    labels=c("Worker",
                                             "Routine non-manual (vs Worker)",
                                             "Lower Service (vs Worker)",
                                             "Higher service (vs Worker)",
                                             "Small business owners and farmers (vs Worker)")),
                       model=fct_rev(factor(plotS,
                                            levels=c("Father's class",
                                                     "Current class",
                                                     "Stable class during BHPS",
                                                     "Stable class over lifetime"),
                                      labels=c("Class as ...Father's class",
                                               "...Current class",
                                               "...Stable class during BHPS",
                                               "...Stable class over lifetime"))))

fig1 %>%  filter(class!="Worker") %>% 
  ggplot(aes(x=model, y=b,ymin=ll1,ymax=ul1,color=model,fill=model)) +
  coord_flip()+
  facet_wrap(~class,ncol=1)+
  geom_hline(aes(yintercept = 0), color="gray20")+
  geom_point()+
  geom_errorbar(size=1,alpha=0.7, width=0,position=position_dodge(width = 0.5)) +
  scale_y_continuous(name="Economic values (right to left)",breaks=seq(-1,0,by=0.2),limits=c(-1,0)) +
  scale_x_discrete(name="") +
  scale_color_brewer(palette="Set1") +
  theme(legend.position="none",
        panel.spacing = unit(1, "lines"),
        panel.grid.major.x = element_line(colour = "grey90"),
        panel.grid.major.y = element_blank(),
        panel.grid.minor = element_blank())

save_plot("figs/appendix_fig1_B6.png",width=12,height=12, dpi=350)
```
### EGP class, Alternative DVs (Appendix B7)

```{r}

for(i in c("a", "b" ,"c" ,"d","e", "f")) {
  
fig1= read_dta(paste0("figs/fig1_",i,"_egp_main_75_all.dta"))

fig1 = fig1 %>% mutate(class=factor(class,levels=c(
                                                1,
                                                2,
                                                3,
                                                4,
                                                5),
                                    labels=c("Worker",
                                             "Routine non-manual (vs Worker)",
                                             "Lower Service (vs Worker)",
                                             "Higher service (vs Worker)",
                                             "Small business owners and farmers (vs Worker)")),
                       model=fct_rev(factor(plotS,
                                            levels=c("Father's class",
                                                     "Current class",
                                                     "Stable class during BHPS",
                                                     "Stable class over lifetime"),
                                      labels=c("Class as ...Father's class",
                                               "...Current class",
                                               "...Stable class during BHPS",
                                               "...Stable class over lifetime"))))

fig1 %>%  filter(class!="Worker") %>% 
  ggplot(aes(x=model, y=b,ymin=ll1,ymax=ul1,color=model,fill=model)) +
  coord_flip()+
  facet_wrap(~class,ncol=1)+
  geom_hline(aes(yintercept = 0), color="gray20")+
  geom_point()+
  geom_errorbar(size=1,alpha=0.7, width=0,position=position_dodge(width = 0.5)) +
  scale_y_continuous(name="Economic values (right to left)",breaks=seq(-2,0,by=0.2),limits=c(-1,0)) +
  scale_x_discrete(name="") +
  scale_color_brewer(palette="Set1") +
  theme(legend.position="none",
        panel.spacing = unit(1, "lines"),
        panel.grid.major.x = element_line(colour = "grey90"),
        panel.grid.major.y = element_blank(),
        panel.grid.minor = element_blank())

save_plot(paste0("figs/appendix_fig1_B7-",i,".png"),width=12,height=12, dpi=350)
}

```

## Figure 2

### EGP class, 3 political generations, raw relationship

```{r}
data= read_dta(paste0("data/ready4analysis.dta"))


fig2raw = data%>% filter(N_red>4 & !is.na(polgen) & !is.na(egp_lifetime75) & !is.na(redz) & birthy>1934 & birthy<1977) %>% 
            group_by(class=egp_lifetime75, gen=polgen, age) %>%   
            mutate(estimate=mean(redz,na.rm=TRUE))

fig2raw = as_tibble(fig2raw)

fig2raw = fig2raw %>% mutate(class=factor(class,levels=c(1,2,3,4,5,120),
                                            labels=c(
                                              "Worker",
                                                "Routine\nnon-manual",
                                                "Lower service",
                                                "Higher service",
                                              "Small\nbusiness own.",
                                              "Other")),
                             gen=factor(gen,labels=c("Early consensus generation (Born 1935-44)",
                                               "Wilson/Callagahan's children (Born 1945-58)",
                                               "Thatcher's children (Born 1959-76)")))

fig2raw %>% 
ggplot(aes(x=age, y=redz)) +
  facet_wrap(~class,ncol=6)+
  geom_hline(aes(yintercept = 0), color="gray20")+
  #geom_ribbon(aes(ymin=min95, ymax=max95),alpha=0.6, fill="gray90",color=NA)+
  #geom_point()+
  #geom_jitter(size=1.1,alpha=0.05) +
  geom_smooth(size=1.1,color="#377eb8") +
  scale_y_continuous(name=paste0("Economic values (right to left)"),breaks=seq(-1.5,1,by=0.25))+
  scale_color_brewer(palette="Set1",guide = guide_legend(reverse = TRUE) )+
  coord_cartesian(ylim=c(-1,0.7))+
  scale_x_continuous(name="Age",breaks=seq(20,60,by=10),limits=c(15,65))+
  theme(legend.position="top")+
  theme(plot.title = element_text(hjust = 0.5),legend.title = element_blank(),
        panel.grid.major = element_line(colour = "grey90"),
        panel.grid.minor = element_blank(),
        panel.spacing.x=unit(0.5, "lines"),
        strip.text = element_text(hjust=0.5))+
  NULL

save_plot("figs/fig2.png",width=18,height=10, dpi=350)


```



## Figure 3

### EGP class, 3 political generations, main specification

```{r}
fig2= read_dta(paste0("figs/fig2_z_egp_main_lifetime75.dta"))

fig2 = fig2 %>% mutate(class=factor(class,levels=c(1,2,3,4,5,6),
                                            labels=c(
                                              "Worker",
                                                "Routine\nnon-manual",
                                                "Lower service",
                                                "Higher service",
                                              "Small\nbusiness own.",
                                              "Other")),
                       gen=factor(gen,labels=c("Early consensus generation (Born 1935-44)",
                                               "Wilson/Callagahan's children (Born 1945-58)",
                                               "Thatcher's children (Born 1959-76)")))

fig2 %>% 
ggplot(aes(x=age, y=estimate, group=gen,color=gen)) +
  facet_wrap(~class,ncol=6)+
  geom_hline(aes(yintercept = 0), color="gray20")+
  geom_ribbon(aes(ymin=min95, ymax=max95),alpha=0.6, fill="gray90",color=NA)+
  #geom_point()+
  geom_line(size=1.1) +
  scale_y_continuous(name=paste0("Economic values (right to left)"),breaks=seq(-1.5,1,by=0.25))+
  scale_color_brewer(palette="Set1",guide = guide_legend(reverse = TRUE) )+
  coord_cartesian(ylim=c(-1,0.7))+
  scale_x_continuous(name="Age",breaks=seq(20,60,by=10),limits=c(15,65))+
  theme(legend.position="top")+
  theme(plot.title = element_text(hjust = 0.5),legend.title = element_blank(),
        panel.grid.major = element_line(colour = "grey90"),
        panel.grid.minor = element_blank(),
        panel.spacing.x=unit(0.5, "lines"),
        strip.text = element_text(hjust=0.5))+
  NULL

save_plot("figs/fig3.png",width=18,height=10, dpi=350)
```

### EGP class, 3 political generations, 90% specification (Appendix C4)

```{r}
fig2= read_dta(paste0("figs/fig2_z_egp_main_lifetime90.dta"))

fig2 = fig2 %>% mutate(class=factor(class,levels=c(1,2,3,4,5,6),
                                            labels=c(
                                              "Worker",
                                                "Routine\nnon-manual",
                                                "Lower service",
                                                "Higher service",
                                              "Small\nbusiness own.",
                                              "Other")),
                       gen=factor(gen,labels=c("Early consensus generation (Born 1935-44)",
                                               "Wilson/Callagahan's children (Born 1945-58)",
                                               "Thatcher's children (Born 1959-76)")))

fig2 %>% 
ggplot(aes(x=age, y=estimate, group=gen,color=gen)) +
  facet_wrap(~class,ncol=6)+
  geom_hline(aes(yintercept = 0), color="gray20")+
  geom_ribbon(aes(ymin=min95, ymax=max95),alpha=0.6, fill="gray90",color=NA)+
  #geom_point()+
  geom_line(size=1.1) +
  scale_y_continuous(name=paste0("Economic values (right to left)"),breaks=seq(-1.5,1,by=0.25))+
  scale_color_brewer(palette="Set1",guide = guide_legend(reverse = TRUE) )+
  coord_cartesian(ylim=c(-1,0.75))+
  scale_x_continuous(name="Age",breaks=seq(20,60,by=10),limits=c(15,65))+
  theme(legend.position="top")+
  theme(plot.title = element_text(hjust = 0.5),legend.title = element_blank(),
        panel.grid.major = element_line(colour = "grey90"),
        panel.grid.minor = element_blank(),
        panel.spacing.x=unit(0.5, "lines"),
        strip.text = element_text(hjust=0.5))+
  NULL

save_plot("figs/appendix_fig3_C4.png",width=18,height=10, dpi=350)

```


### EGP class, 3 political generations, panel specification (Appendix C5)

```{r}
fig2= read_dta(paste0("figs/fig2_z_egp_main_panel75.dta"))

fig2 = fig2 %>% mutate(class=factor(class,levels=c(1,2,3,4,5,6),
                                            labels=c(
                                              "Worker",
                                                "Routine\nnon-manual",
                                                "Lower service",
                                                "Higher service",
                                              "Small\nbusiness own.",
                                              "Other")),
                       gen=factor(gen,labels=c("Early consensus generation (Born 1935-44)",
                                               "Wilson/Callagahan's children (Born 1945-58)",
                                               "Thatcher's children (Born 1959-76)")))

fig2 %>% 
ggplot(aes(x=age, y=estimate, group=gen,color=gen)) +
  facet_wrap(~class,ncol=6)+
  geom_hline(aes(yintercept = 0), color="gray20")+
  geom_ribbon(aes(ymin=min95, ymax=max95),alpha=0.6, fill="gray90",color=NA)+
  #geom_point()+
  geom_line(size=1.1) +
  scale_y_continuous(name=paste0("Economic values (right to left)"),breaks=seq(-1.5,1,by=0.25))+
  scale_color_brewer(palette="Set1",guide = guide_legend(reverse = TRUE) )+
  coord_cartesian(ylim=c(-1,0.7))+
  scale_x_continuous(name="Age",breaks=seq(20,60,by=10),limits=c(15,65))+
  theme(legend.position="top")+
  theme(plot.title = element_text(hjust = 0.5),legend.title = element_blank(),
        panel.grid.major = element_line(colour = "grey90"),
        panel.grid.minor = element_blank(),
        panel.spacing.x=unit(0.5, "lines"),
        strip.text = element_text(hjust=0.5))+
  NULL

save_plot("figs/appendix_fig3_C5.png",width=18,height=10, dpi=350)
```

### EGP class, 5 cohorts, main specification (Appendix C6)

```{r}
fig2= read_dta(paste0("figs/fig2_z_egp_lifetime75_main_gen5.dta"))


fig2 = fig2 %>% mutate(class=factor(class,levels=c(1,2,3,4,5,6),
                                            labels=c(
                                              "Worker",
                                                "Routine\nnon-manual",
                                                "Lower service",
                                                "Higher service",
                                              "Small\nbusiness owners",
                                              "Other")),
                       gen=factor(gen,labels=c("1935-43",
                                               "1944-52",
                                               "1953-60",
                                               "1961-68",
                                               "1969-76")))

fig2 %>% 
ggplot(aes(x=age, y=estimate, group=gen,color=gen)) +
  facet_wrap(~class,ncol=6)+
  geom_hline(aes(yintercept = 0), color="gray20")+
  geom_ribbon(aes(ymin=min95, ymax=max95),alpha=0.6, fill="gray90",color=NA)+
  #geom_point()+
  geom_line(size=1.1) +
  scale_y_continuous(name=paste0("Economic values (right to left)"),breaks=seq(-1.5,1,by=0.25))+
  scale_color_brewer(palette="Set1",guide = guide_legend(reverse = TRUE) )+
  coord_cartesian(ylim=c(-1,0.75))+
  scale_x_continuous(name="Age",breaks=seq(20,60,by=10),limits=c(15,65))+
  theme(legend.position="top")+
  theme(plot.title = element_text(hjust = 0.5),legend.title = element_blank(),
        panel.grid.major = element_line(colour = "grey90"),
        panel.grid.minor = element_blank(),
        panel.spacing.x=unit(0.5, "lines"),
        strip.text = element_text(hjust=0.5))+
  NULL


save_plot("figs/appendix_fig3_C6.png",width=18,height=10, dpi=350)


```

### EGP class, 3 political generations, restrict sample to only include observations after 8 years in employment

```{r}
fig2= read_dta(paste0("figs/fig2_z_egp_main_lifetime75res.dta"))

fig2 = fig2 %>% mutate(class=factor(class,levels=c(1,2,3,4,5,6),
                                            labels=c(
                                              "Worker",
                                                "Routine\nnon-manual",
                                                "Lower service",
                                                "Higher service",
                                              "Small\nbusiness own.",
                                              "Other")),
                       gen=factor(gen,labels=c("Early consensus generation (Born 1935-44)",
                                               "Wilson/Callagahan's children (Born 1945-58)",
                                               "Thatcher's children (Born 1959-76)")))

fig2 %>% 
ggplot(aes(x=age, y=estimate, group=gen,color=gen)) +
  facet_wrap(~class,ncol=6)+
  geom_hline(aes(yintercept = 0), color="gray20")+
  geom_ribbon(aes(ymin=min95, ymax=max95),alpha=0.6, fill="gray90",color=NA)+
  #geom_point()+
  geom_line(size=1.1) +
  scale_y_continuous(name=paste0("Economic values (right to left)"),breaks=seq(-1.5,1,by=0.25))+
  scale_color_brewer(palette="Set1",guide = guide_legend(reverse = TRUE) )+
  coord_cartesian(ylim=c(-1,0.7))+
  scale_x_continuous(name="Age",breaks=seq(20,60,by=10),limits=c(15,65))+
  theme(legend.position="top")+
  theme(plot.title = element_text(hjust = 0.5),legend.title = element_blank(),
        panel.grid.major = element_line(colour = "grey90"),
        panel.grid.minor = element_blank(),
        panel.spacing.x=unit(0.5, "lines"),
        strip.text = element_text(hjust=0.5))+
  NULL

save_plot("figs/appendix_fig3_C7.png",width=18,height=10, dpi=350)

```



# Figure D.1

### EGP class, main specification

```{r}

fig3a= read_dta(paste0("figs/figD1_egp_combined.dta"))
fig3a = fig3a %>% mutate(treat=as_factor(treat))

fig3a %>% 
  filter(treat!="Non-treated") %>% 
  #filter(wave<5) %>% 
  ggplot(aes(x=year, y=estimate,ymin=min95,ymax=max95)) +
  facet_wrap(~treat,ncol=2,scales="free_x")+
  geom_hline(aes(yintercept = 0), color="gray20")+
  geom_ribbon(alpha=0.2)+
  geom_point(size=2,color="#e41a1c")+
  geom_line(size=1,color="#e41a1c")+
  #geom_errorbar(size=1,alpha=0.7, width=0,position=position_dodge(width = 0.5)) +
  scale_y_continuous(name="Economic values (right to left)",breaks=seq(-2,2,by=0.05)) +
  scale_x_continuous(name="Years after mobility") +
  scale_color_brewer(palette="Set1") +
  theme(legend.position="none",
        panel.spacing = unit(1, "lines"),
        panel.grid.major.y = element_line(colour = "grey90"),
        panel.grid.major.x = element_blank(),
        panel.grid.minor = element_blank())

save_plot("figs/fig3a_egp_main_basic.png",width=16,height=10)


down=fig3a %>%
  filter(treat=="Downwardly mobile") %>% 
  #filter(wave<5) %>% 
  ggplot(aes(x=year, y=estimate,ymin=min95,ymax=max95)) +
  geom_hline(aes(yintercept = 0), color="gray20")+
  geom_ribbon(alpha=0.2)+
  geom_point(size=2,color="#e41a1c")+
  geom_line(size=1,color="#e41a1c")+
  #geom_errorbar(size=1,alpha=0.7, width=0,position=position_dodge(width = 0.5)) +
  scale_y_continuous(name=NULL,breaks=seq(-0.75,1,by=0.05)) +
  scale_x_continuous(name=NULL,labels=scales::number_format(accuracy=1)) +
  scale_color_brewer(palette="Set1") +
  theme(legend.position="none",
        plot.background = element_rect(color = "black", size = 1),
        panel.spacing = unit(1, "lines"),
        panel.grid.major.y = element_line(colour = "grey90"),
        panel.grid.major.x = element_blank(),
        panel.grid.minor = element_blank())

up=fig3a %>%
  filter(treat=="Upwardly mobile") %>% 
  #filter(wave<5) %>% 
  ggplot(aes(x=year, y=estimate,ymin=min95,ymax=max95)) +
  geom_hline(aes(yintercept = 0), color="gray20")+
  geom_ribbon(alpha=0.2)+
  geom_point(size=2,color="#e41a1c")+
  geom_line(size=1,color="#e41a1c")+
  #geom_errorbar(size=1,alpha=0.7, width=0,position=position_dodge(width = 0.5)) +
  scale_y_continuous(name=NULL,breaks=seq(-1,1,by=0.05)) +
  scale_x_continuous(name=NULL,labels=scales::number_format(accuracy=1)) +
  scale_color_brewer(palette="Set1") +
  theme(legend.position="none",
        plot.background = element_rect(color = "black", size = 1),
        panel.spacing = unit(1, "lines"),
        panel.grid.major.y = element_line(colour = "grey90"),
        panel.grid.major.x = element_blank(),
        panel.grid.minor = element_blank())

main=fig3a %>% 
  filter(treat!="Non-treated") %>% 
  #filter(wave<5) %>% 
  ggplot(aes(x=year, y=estimate,ymin=min95,ymax=max95)) +
  facet_wrap(~treat,ncol=1,scales="free_x")+
  geom_segment(aes(x=-Inf,xend=8,y=0,yend=0), color="gray20")+
  geom_segment(aes(x=-Inf,xend=8,y=0.5,yend=0.5), color="gray90")+
  geom_segment(aes(x=-Inf,xend=8,y=0.25,yend=0.25), color="gray90")+
  geom_segment(aes(x=-Inf,xend=8,y=-0.25,yend=-0.25), color="gray90")+
  geom_segment(aes(x=-Inf,xend=8,y=-0.5,yend=-0.5), color="gray90")+
  geom_segment(aes(x=-Inf,xend=8,y=-0.75,yend=-0.75), color="gray90")+
  geom_segment(aes(x=-Inf,xend=8,y=-1,yend=-1), color="gray90")+
  geom_segment(aes(x=-Inf,xend=8,y=-1.25,yend=-1.25), color="gray90")+
  geom_segment(aes(x=-Inf,xend=8,y=-1.5,yend=-1.5), color="gray90")+
  geom_segment(aes(x=8.5,xend=8.2,y=-0.1,yend=0), color="black",arrow = arrow(length = unit(0.2, "cm")))+
  geom_ribbon(alpha=0.2)+
  geom_point(size=2,color="#e41a1c")+
  geom_line(size=1,color="#e41a1c")+
  #geom_errorbar(size=1,alpha=0.7, width=0,position=position_dodge(width = 0.5)) +
  scale_y_continuous(name="Economic values (right to left)",breaks=seq(-1,1,by=0.25)) +
  coord_cartesian(xlim=c(0,12),ylim=c(-1,0.75))+
  scale_x_continuous(name="Years after mobility",breaks=c(0,1,2,3,4,5,6,7,8)) +
  scale_color_brewer(palette="Set1") +
  theme(legend.position="none",
        panel.spacing = unit(1, "lines"),
        panel.grid.major.y = element_blank(),
        axis.title.x = element_text(hjust = 0),
        panel.grid.minor = element_blank())

f3=ggdraw(main) +
  draw_plot(down, .7, .6, .25, .25)+
  draw_plot(up, .7, .01, .25, .25)

cowplot::save_plot("figs/appendix_figD1.jpg",f3,base_width=8,base_height=6,bg = "white", dpi=350)


```


